package leecode;

/**
 *
 *
 * @author qingyan 2021-10-28
 */
public class Question35 {

    public int searchInsert(int[] nums, int target) {
        if (nums.length < 1) {
            return 0;
        }
        int l = 0, r = nums.length - 1;
        while (l < r) {
            int mid = (r + l) / 2;
            if (nums[mid] == target) {
                return mid;
            } else if (target < nums[mid]) {
                r = mid;
            } else {
                l = mid + 1;
            }
        }
        if (target <= nums[l]) {
            return l;
        } else {
            return l + 1;
        }
    }

    public static void main(String[] args) {
        Question35 question35 = new Question35();
        System.out.println(question35.searchInsert(new int[]{1, 2, 4, 5}, 6));
    }
}
